package org.i1510.wxflow.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.service.impl.BaseServiceImpl;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.wxflow.dao.IWxFlowTplDao;
import org.i1510.wxflow.po.WxFlowTpl;
import org.i1510.wxflow.service.IWxFlowTplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("iWxFlowTplService")
public class WxFlowTplServiceImpl extends BaseServiceImpl implements IWxFlowTplService{
    @Autowired
    private IWxFlowTplDao iWxFlowTplDao;    
    
    public ResultMapper save(WxFlowTpl basePo,HttpServletRequest request) {
        ResultMapper mapper = ResultMapper.generateFailure();
        WxFlowTpl saved = null;
        if(null !=basePo && basePo.getDocId() != null  && !basePo.getDocId().equals("")){//修改
            WxFlowTpl finder = iWxFlowTplDao.findById(basePo.getDocId());   
            if(null != finder){
                finder.setDocLastTime(DateUtils.getNow());
                finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());            
                BeanUtils.copy(finder, basePo);
                saved = iWxFlowTplDao.save(finder); 
            }else{
                mapper = ResultMapper.generateFailure();
                return mapper;
            }
            
        }else{//新增
            basePo.setDocId(IDGenerator.getGenerateID());
            basePo.setDocCreateTime(DateUtils.getNow());
            basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
            saved = iWxFlowTplDao.save(basePo);
        }
        if (saved == null) {
            mapper = ResultMapper.generateFailure();
            return mapper;
        }else{
            mapper = ResultMapper.generateSuccess();
            return mapper;            
        }
    }
    public ResultMapper delete(String docId) {
        ResultMapper mapper = ResultMapper.generateFailure();
        iWxFlowTplDao.delete(docId);
        mapper = ResultMapper.generateSuccess();
        return mapper;
    }
    
    public WxFlowTpl findById(String docId){
        return  iWxFlowTplDao.findById(docId);
    }
    
    public List<WxFlowTpl> findByIds(List<String> docIds){
        return (List<WxFlowTpl>) iWxFlowTplDao.findAll(docIds);
    }
    public List getPage(WxFlowTpl wxFlowTpl,PageBean pageBean,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(wxFlowTpl, map);
        map.put("intPageIndex", pageBean.getIntPageIndex());
        map.put("intPageSize", pageBean.getIntPageSize());
        map.putAll(conditions);
        List list = this.queryForListBySql("wxFlowTplSql.getPage", map);
        return list;
    }
    public int getPageCount(WxFlowTpl wxFlowTpl,Map conditions) {
        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(wxFlowTpl, map);
        map.putAll(conditions);
        List list = this.queryForListBySql("wxFlowTplSql.getPageCount", map);        
        return list.size();
    }
}
